using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;
using ProgramMain.Skins;

namespace ProgramMain.Framework
{
    public partial class frmWaiting : DevExpress.XtraEditors.XtraForm
    {
        private static QueueWaitingThread WaitingThread = null;
        private static int fWaitingCount = 0;

        public frmWaiting()
        {
            InitializeComponent();
        }

        protected override CreateParams CreateParams
        {
            get
            {
                const int WS_EX_NOACTIVATE = 0x08000000;
                const int WS_EX_TOOLWINDOW = 0x00000080;
                CreateParams baseParams = base.CreateParams;

                baseParams.ExStyle |= (int)(WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);

                return baseParams;
            }
        }

        public static void StopWaitingDialogThread()
        {
            if (WaitingThread != null)
            {
                WaitingThread.Dispose();
                WaitingThread = null;
                fWaitingCount = 0;
            }
        }

        public static void ShowWaiting()
        {
            if (WaitingThread == null)
            {
                WaitingThread = new QueueWaitingThread();
                fWaitingCount = 0;
            }

            if (fWaitingCount == 0)
            {
                WaitingThread.PostWaitingMsg(QueueWaitingMessage.MsgType.ShowWaiting);
            }
            fWaitingCount++;
        }

        public static void HideWaiting()
        {
            if (WaitingThread != null)
            {
                fWaitingCount--;
                
                if (fWaitingCount <= 0)
                {
                    WaitingThread.PostWaitingMsg(QueueWaitingMessage.MsgType.HideWaiting);
                    
                    /*WaitingThread.Dispose();
                    WaitingThread = null;*/

                    fWaitingCount = 0;
                }
            }
        }

        private void frmWaiting_Activated(object sender, EventArgs e)
        {
            if (Program.MainForm != null)
            {
                Screen mainSC = null;
                foreach (Screen sc in Screen.AllScreens)
                {
                    if (sc.DeviceName == Program.MainForm.ScreenDeviceName)
                    {
                        mainSC = sc;
                        break;
                    }
                }
                if (mainSC != null && !mainSC.WorkingArea.Contains(this.Location))
                {
                    int Left = (Program.MainForm.Right + Program.MainForm.Left - this.Width) / 2;
                    int Top = (Program.MainForm.Bottom + Program.MainForm.Left - this.Height) / 2;

                    this.Location = new Point(Left, Top);
                }
            }
        }
    }

    //sample to using messages in WorkerThread------------------------------------
    internal class QueueWaitingMessage : QueueMessageBase
    {
        public enum MsgType { ShowWaiting = 0, HideWaiting = 1};

        public QueueWaitingMessage(MsgType MsgInfo)
            : base(Convert.ToInt32(MsgInfo))
        {
        }
    }

    internal static class MsgTypeConverter
    {
        public static Array _MsgTypeEnum = null;
        public static Array MsgTypeEnum
        {
            get
            {
                if (_MsgTypeEnum == null)
                    _MsgTypeEnum = Enum.GetValues(typeof(QueueWaitingMessage.MsgType));
                return _MsgTypeEnum;
            }
        }

        public static QueueWaitingMessage.MsgType ToMsgType(this Int32 i)
        {
            if (i >= 0 && i < MsgTypeEnum.Length)
                return (QueueWaitingMessage.MsgType)Convert.ChangeType(MsgTypeEnum.GetValue(i), typeof(QueueWaitingMessage.MsgType));
            else
                return QueueWaitingMessage.MsgType.HideWaiting;
        }
    }

    internal class QueueWaitingThread : QueueMessageThread
    {
        private frmWaiting frmWaiting = null;

        public QueueWaitingThread()
            : base(null, ThreadPriority.AboveNormal)
        {
            CreateThread();
        }

        public void PostWaitingMsg(QueueWaitingMessage.MsgType MsgInfo)
        {
            PostMessage(new QueueWaitingMessage(MsgInfo));
        }

        protected override void OnThreadStart()
        {
            ApplicationSkins.EnableSkins();
            ApplicationSkins.ResotreSkinSettings();
        }

        protected override bool TranslateMessage(int Param, QueueMessageBase Message)
        {
            if (Message is QueueWaitingMessage)
            {
                QueueWaitingMessage.MsgType msgType = Param.ToMsgType();
                switch (msgType)
                {
                    case QueueWaitingMessage.MsgType.ShowWaiting:
                        {
                            if (frmWaiting == null)
                            {
                                frmWaiting = new frmWaiting();
                                this.MainForm = frmWaiting;
                            }
                            frmWaiting.Show();
                        }
                        break;
                    case QueueWaitingMessage.MsgType.HideWaiting:
                        {
                            if (frmWaiting != null)
                            {
                                frmWaiting.Hide();
                                //frmWaiting.Close();
                            }
                        }
                        break;
                }
            }
            return base.TranslateMessage(Param, Message);
        }
    }
}